<?php

namespace App\Console\Commands;

use App\Models\Family;
use App\Models\Project;
use App\Models\Team;
use App\Models\User;
use App\Models\UserProject;
use App\Models\Volunteer;
use App\Models\WechatMsg\AddReward;
use App\Models\WechatMsg\ApplyProject;
use App\Models\WechatMsg\ApplyReward;
use App\Models\WechatMsg\ClockReward;
use App\Models\WechatMsg\JoinTeam;
use App\Models\WechatMsg\RecApplyReward;
use App\Models\WechatMsg\RecClock;
use App\Models\WechatMsg\RecJoinProject;
use App\Models\WechatMsg\RecJoinTeam;
use App\Models\WechatMsg\VerifyProject;
use App\Models\WechatTempMsg;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use EasyWeChat\Foundation\Application;

class WechatMsg extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sendWechatMsg';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新家庭成员';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        Log::info('send wechat msg');

        $nodes = WechatTempMsg::query()->whereRaw('push_time is  null')->orderBy('id', 'desc')->get();

        foreach($nodes as $node)
        {
            try{
                $content = $node->content;

                switch ($node->type)
                {
                    case 1:
                    case 2:
                        $obj = new ApplyProject($node->type, $content);

                        break;

                    case 3:
                    case 4:
                        $obj = new JoinTeam($node->type, $content);
                        break;

                    case 5:
                        $obj = new ClockReward($node->type, $content);
                        break;

                    case 6:
                        $obj = new AddReward($node->type, $content);
                        break;

                    case 7:
                    case 8:
                        $obj = new ApplyReward($node->type, $content);
                        break;

                    case 50:
                        $obj = new RecJoinProject($node->type, $content);
                        break;

                    case 51:
                        $obj = new RecClock($node->type, $content);
                        break;

                    case 52:
                        $obj = new RecApplyReward($node->type, $content);
                        break;

                    case 53:
                        $obj = new VerifyProject($node->type, $content);
                        break;

                    case 54:
                        $obj = new VerifyProject($node->type, $content);
                        break;

                    case 56:
                        $obj = new RecJoinTeam($node->type, $content);
                        break;
                }

                $conf = $obj->getOrgWechatConfig();
                $app = new Application($conf['options']);

                $notice = $app->notice;
            }catch (\Exception $e)
            {
                Log::error($e->getMessage());
            }


            //$messageId = $notice->to($userOpenId)->url($url)->template($templateId)->andData($data)->send();

            try{
                $messageId = $notice->to($conf['openId'])->template($conf['templateId'])->url($conf['url'])->andData($obj->getMessageData())->send();
            }catch (\Exception $e)
            {
                Log::error($e->getMessage());
            }

            $node->push_time = date('Y-m-d H:i:s');
            $node->save();


        }
    }
}
